package com.faker.dba.controller;

import com.faker.dba.model.dto.GeTableInfoDto;
import com.faker.dba.model.dto.GeTableListDto;
import com.faker.dba.model.dto.GetDatabaseInfoDto;
import com.faker.dba.model.vo.LayuiColVo;
import com.faker.dba.model.vo.LayuiPageDataVo;
import com.faker.dba.service.DataBaseService;
import com.faker.dba.wrapper.AesWrapMapper;
import com.faker.dba.wrapper.Wrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Api("数据库控制层")
@Slf4j
@Controller
@RequestMapping(value = "dba")
public class DataBaseController extends BaseController {

    /**
     * 数据库服务层
     */
    @Autowired
    private DataBaseService dataBaseService;

    @ResponseBody
    @ApiOperation("获取数据库信息")
    @PostMapping(value = "/getDatabaseInfo")
    public Wrapper getDatabaseInfo(@RequestBody GetDatabaseInfoDto params) {
        log.info(super.postMethodLog());
        log.info(params.toString());
        try {
            return dataBaseService.getDatabaseInfo(params);
        } catch (Exception e) {
            log.error(super.getApiOperationTitle() + "错误", e);
            return AesWrapMapper.error(super.getApiOperationTitle() + "错误：" + e.getMessage());
        }
    }

    @ResponseBody
    @ApiOperation("获取表结构信息")
    @PostMapping(value = "/geTableInfo")
    public Wrapper<List<LayuiColVo>> geTableInfo(@RequestBody GeTableInfoDto params) {
        log.info(super.postMethodLog());
        log.info(params.toString());
        try {
            return dataBaseService.geTableInfo(params);
        } catch (Exception e) {
            log.error(super.getApiOperationTitle() + "错误", e);
            return AesWrapMapper.error(super.getApiOperationTitle() + "错误：" + e.getMessage());
        }
    }

    @ResponseBody
    @ApiOperation("获取表数据信息")
    @PostMapping(value = "/geTableList")
    public LayuiPageDataVo geTableList(@RequestBody GeTableListDto params) {
        log.info(super.postMethodLog());
        log.info(params.toString());
        try {
            return dataBaseService.geTableList(params);
        } catch (Exception e) {
            log.error(super.getApiOperationTitle() + "错误", e);
            LayuiPageDataVo layuiPageVo = new LayuiPageDataVo();
            layuiPageVo.setCode(500);
            layuiPageVo.setMsg(super.getApiOperationTitle() + "错误：" + e.getMessage());
            return layuiPageVo;
        }
    }
}
